local game = require "club.threecard.hbsl"
local GameObjectExtension = CS.Blaze.GameObjectExtension

local M = game.ui.element()

local no = 
{
    id = -1,
    gameobject = nil,
    selected_obj = nil,
    unselected_obj = nil,
    panel = nil,
    selected = false,

    on_select = function(self, b)
        game.store.gameplay.set_no(self.id)
        self.selected = b
        self.selected_obj:SetActive(b)
        self.unselected_obj:SetActive(not b)
    end,

    on_selected_click = function(self)
        self:on_select(false)
    end,

    on_unselected_click = function(self)
        for _, item in pairs(self.panel.nos) do
            if item.selected then
                item:on_select(false)
            end
        end

        self:on_select(true)
    end,

    on_release = function(self)
        GameObjectExtension.ReturnToPool(self.gameobject)
        self.selected_listener.PointerClick:RemoveAllListeners()
        self.unselected_listener.PointerClick:RemoveAllListeners()
        self.selected_obj = nil
        self.unselected_obj = nil
        self.selected_listener = nil
        self.unselected_listener = nil
    end
}

local no_mt = {__index = no}

local function create_no(obj, id, panel)
    local ret = {}
    ret.id = id
    ret.gameobject = obj
    ret.selected_obj = obj.transform:Find("selected").gameObject
    ret.unselected_obj = obj.transform:Find("unselected").gameObject
    ret.no_txt = obj.transform:Find("no_txt"):GetComponent(typeof(CS.UnityEngine.UI.Text))
    ret.no_txt.text = id
    ret.panel = panel
    ret.selected = false

    ret.selected_obj:SetActive(false)
    ret.unselected_obj:SetActive(true)

    ret.selected_listener = ret.selected_obj:GetComponent(typeof(CS.Blaze.UI.EventTriggerListener))
    ret.unselected_listener = ret.unselected_obj:GetComponent(typeof(CS.Blaze.UI.EventTriggerListener))

    ret.selected_listener.PointerClick:AddListener(function() ret:on_selected_click() end)
    ret.unselected_listener.PointerClick:AddListener(function() ret:on_unselected_click() end)

    setmetatable(ret, no_mt)
    return ret
end

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:send_panel"

    local packet_queue = game.require 'ui.ingame.packet_queue'
    self.packet_queue = packet_queue.new()

    self.nos = {}

    local bet_operator = game.require "ui.ingame.bet_operator"
    self.bet_operator = bet_operator.new()
end

function M:show()
    self:super("show")
    self.packet_queue:show()
end

function M:on_load()
    self:super("on_load")

    self.packet_queue:set_parent(self.packet_queue_root, false)
    self.packet_queue:load()

    self.bet_operator:set_parent(self.bet_operator_root, false)
    self.bet_operator:load()
end

function M:on_injected(bind)
    bind("button.click", self.btn_close, self.close_ivk)
    bind("button.click", self.btn_mask, self.close_ivk)
    bind("button.click", self.btn_confirm, self.confirm_ivk)
    bind("button.click", self.btn_add, self.add_times_ivk)
    bind("button.click", self.btn_sub, self.sub_times_ivk)
    bind("slider.value", self.slider, self.slider_times_ivk)

    game.utility.button_style.highlight(self.btn_close)
    game.utility.button_style.highlight(self.btn_confirm)
    game.utility.button_style.highlight(self.btn_add)
    game.utility.button_style.highlight(self.btn_sub)

    for i = 0, 9 do
        local no_obj = GameObjectExtension.GetFromPool(self.no_temp)
        no_obj.transform:SetParent(self.no_root, false)

        local no = create_no(no_obj, i, self)
        table.insert(self.nos, no)
    end

    self.slider.minValue = 1
    self.slider.maxValue = 10
    self.slider.value = 1

    self:update_times()
end

function M:update_times()
    local amount = math.floor(self.slider.value)
    self.auto_txt.text = amount
end

function M:close_ivk()
    self:hide()
    game.audio.post("hbsl:click")
end

function M:confirm_ivk()
    game.audio.post("hbsl:click")
    game.store.gameplay.send_packet()
end

function M:add_times_ivk()
    local value = self.slider.value + 1
    if value > self.slider.maxValue then
        value = self.slider.minValue
    end

    self.slider.value = value
    self:update_times()
end

function M:sub_times_ivk()
    local value = self.slider.value - 1
    if value <= 0 then
        value = self.slider.maxValue
    end

    self.slider.value = value
    self:update_times()

    game.audio.post("hbsl:click")
end

function M:slider_times_ivk()
    self:update_times()
end

function M:on_release()
    self:super("on_release")
    for _, item in pairs(self.nos) do
        item:on_release()
    end

    self.nos = {}
end

return M